package a;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: jzw
 * @Description
 * @date: 2022/9/1 21:14
 */
public class JsonToCount {
    public static void main(String[] args) throws IOException, IllegalAccessException {
        // 获取文件的输入流对象
        /*FileInputStream fileInputStream = new FileInputStream("F:\\workspace\\jsonlocal.json");
        int len;
        byte[] bytes = new byte[1024];
        StringBuilder stringBuffer = new StringBuilder();
        while ((len = fileInputStream.read(bytes)) != -1) {
            // 添加字符串到缓冲区
            stringBuffer.append(new String(bytes, 0, len));
        }
        // 关闭资源
        fileInputStream.close();
        String s = stringBuffer.toString().trim();
        String substring = s.substring(1, s.length() - 1);

        System.out.println(substring);

        String[] split = substring.split(",");

        System.out.println("字段个数："+split.length);

        for (String s1 : split){
            System.out.println(s1);
        }*/
        String s = "{\"OpType\":\"A\",\"UserID\":\"9311000832338832\",\"PaymentMethod\":\"2\",\"Province\":\"HB\",\"City\":\"十堰\",\"AreaCode\":\"0719\",\"IMSI\":\"460040965901336\",\"MSISDN\":\"1064896591336\",\"ICCID\":\"898602B9171996911336\",\"IoVCardType\":\"3\",\"CustomerStatus\":\"20\",\"StatusUpdateTime\":\"20221107110614\",\"AcceptTime\":\"20220828120648\",\"ActivationTime\":\"20220828120648\",\"FirstCallTime\":\"20220828120648\",\"ChannelType\":\"1\",\"LogginginAddress\":\"270719010001883\",\"NodeCode\":\"20181128000103\",\"ChannelName\":\"20181128000103\",\"StaffID\":\"hw_gaoxw_hb\",\"ServiceType\":\"[\\\"车联网个人\\\"]\",\"Voice\":\"0\",\"Message\":\"1\",\"Data\":\"1\",\"VoiceLimType\":\"\",\"VoiceDirNum\":\"\",\"VoiceDirList\":\"[\\\"\\\"]\",\"MessageLimType\":\"2\",\"MessageDirNum\":\"1\",\"MessageDirList\":\"[\\\"1064899170128\\\"]\",\"DataLimType\":\"3\",\"DataLimit\":\"100\",\"DataDirLimit\":\"\",\"DataDirNum\":\"\",\"DataDirList\":\"[\\\"\\\"]\",\"BindState\":\"1\",\"BindType\":\"1\",\"CompanyName\":\"湖北东裕汽车服务有限公司\",\"CompanyCertType\":\"I\",\"CompanyCertNumber\":\"91420100755146454R\",\"CompanyCertAddress\":\"武汉经济技术开发区三角湖路3号东合中心B座1204室\",\"CompanyPosterAddress\":\"武汉经济技术开发区三角湖路3号东合中心B座1204室\",\"OperatorName\":\"贾珺杰\",\"OperatorCertType\":\"A\",\"OperatorCertNumber\":\"420303197311212867\",\"OperatorCertAddress\":\"武汉市汉阳区东风大道10号\",\"OperatorPosterAddress\":\"武汉市汉阳区东风大道10号\",\"EndOperatorTelephone\":\"\",\"CorporationName\":\"贾珺杰\",\"CorporationCertType\":\"A\",\"CorporationCertNumber\":\"420303197311212867\",\"CorporationCertAddress\":\"武汉市汉阳区东风大道10号\",\"CorporationPosterAddress\":\"武汉市汉阳区东风大道10号\",\"CorporationTelephone\":\"\",\"SalesTime\":\"20220828120648\",\"DataAPNNum\":\"2\",\"DataAPNList\":\"[\\\"CMIOT\\\",\\\"CMNBIOT1\\\"]\",\"CorporateCardholderName\":\"贾珺杰\",\"CorporateCardholderCertType\":\"A\",\"CorporateCardholderNumber\":\"420303197311212867\",\"CorporateCardholderAddress\":\"武汉市汉阳区东风大道10号\",\"CorporateCardholderDate\":\"20220305-30000101\",\"CorporateCardholderPosterAddress\":\"武汉市汉阳区东风大道10号\"}";
        JSONObject object = JSON.parseObject(s);
    }

    public static void jsonToObject(String json, Map<String, Integer> map) throws IllegalAccessException {
        JSONObject jsonObject = JSONObject.parseObject(json);
        Account account = JSONObject.toJavaObject(jsonObject, Account.class);
        ArrayList<String> required = new ArrayList<>();
        ArrayList<String> notRequired = new ArrayList<>();
        if (57 != jsonObject.size()) {
            System.out.println("字段数量不符合,字段数量: " + jsonObject.size());
        }
        System.out.println(json);
        Field[] fields = account.getClass().getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            Object value = field.get(account);
            //属性值为""或null，在判断该属性名为必填或选填,将选填或必填字段放入list
            //value = null 增加判断
            String value2 = (String)value;
            if (value2.startsWith("[")) {
                String newValue = value2.substring(1, value2.length() - 1);
                value = ",".split(newValue);
            }
            if (value.equals("")) {
                List<String> noRequiredName = Account.getFieldMList();//选填
                if (!noRequiredName.contains(name)) {
                    required.add(name);
                } else if ("VoiceLimType".equals(name)) {
                    Object valueByName = getFieldValueByName("Data", account);
                    assert valueByName != null;
                    if (valueByName.equals("1")) {
                        required.add(name);
                    }
                } else if ("VoiceDirNum".equals(name)) {
                    Object valueByName = getFieldValueByName("VoiceLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2")) {
                        required.add(name);
                    }
                } else if ("VoiceDirList".equals(name)) {
                    Object valueByName = getFieldValueByName("VoiceLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2")) {
                        required.add(name);
                    }
                } else if ("MessageLimType".equals(name)) {
                    Object valueByName = getFieldValueByName("Message", account);
                    assert valueByName != null;
                    if (valueByName.equals("1")) {
                        required.add(name);
                    }
                } else if ("MessageDirNum".equals(name)) {
                    Object valueByName = getFieldValueByName("MessageLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2")) {
                        required.add(name);
                    }
                } else if ("MessageDirList".equals(name)) {
                    Object valueByName = getFieldValueByName("MessageLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2")) {
                        required.add(name);
                    }
                } else if ("DataLimType".equals(name)) {
                    Object valueByName = getFieldValueByName("Data", account);
                    assert valueByName != null;
                    if (valueByName.equals("1")) {
                        required.add(name);
                    }
                } else if ("DataStartDate".equals(name)) {
                    Object valueByName = getFieldValueByName("DataLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("1") || valueByName.equals("3") || valueByName.equals("5")) {
                        required.add(name);
                    }
                } else if ("DataLimit".equals(name)) {
                    Object valueByName = getFieldValueByName("DataLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("1") || valueByName.equals("3") || valueByName.equals("5")) {
                        required.add(name);
                    }
                } else if ("DataDirStartDate".equals(name)) {
                    Object valueByName = getFieldValueByName("DataLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2") || valueByName.equals("4") || valueByName.equals("5")) {
                        required.add(name);
                    }
                } else if ("DataDirLimit".equals(name)) {
                    Object valueByName = getFieldValueByName("DataLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2") || valueByName.equals("4") || valueByName.equals("5")) {
                        required.add(name);
                    }
                } else if ("DataDirNum".equals(name)) {
                    Object valueByName = getFieldValueByName("DataLimType", account);
                    assert valueByName != null;
                    if (valueByName.equals("2") || valueByName.equals("4") || valueByName.equals("5")) {
                        required.add(name);
                    }
                } else if ("BindType".equals(name)) {
                    Object valueByName = getFieldValueByName("BindState", account);
                    assert valueByName != null;
                    if (valueByName.equals("1")) {
                        required.add(name);
                    }
                }
                notRequired.add(name);
            }
//            System.out.println("属性名：" + field.getName() + "\t属性值：" + field.get(account));
        }
        System.out.println("根据场景条件必填字段为空数量：" + required.size());
        for (String name : required) {
            System.out.print(name + "  ");
        }
        System.out.println("\n选填字段为空数量：" + notRequired.size());
        for (String name : notRequired) {
            System.out.print(name + "  ");
        }
    }

    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
